package problem;

import java.util.Arrays;

/**
 * @author RunningShrimp
 * @date 2021/5/27  20:35
 * @see <a href="https://leetcode-cn.com/problems/range-sum-query-immutable/">303. 区域和检索 - 数组不可变</a>
 */
public class RangeSumQueryImmutable {
    static class NumArray {
        private final int[] sum;
        public NumArray(int[] nums) {
            sum = new int[nums.length + 1];
            for (int i = 1; i < sum.length; i++) {
                sum[i] = nums[i - 1] + sum[i - 1];
            }
        }
        public int sumRange(int left, int right) {
            return sum[right] - sum[left];
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("NumArray{");
            sb.append("sum=").append(Arrays.toString(sum));
            sb.append('}');
            return sb.toString();
        }
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        // 0 -2 -2 1 -4 -2 -3
        NumArray numArray= new NumArray(nums);
        System.out.println(numArray);
        nums = new int[]{0, 2};
        numArray = new NumArray(nums);
        System.out.println(numArray);

        nums = new int[]{2, 5};
        numArray = new NumArray(nums);
        System.out.println(numArray);

        nums = new int[]{0, 5};
        numArray = new NumArray(nums);
        System.out.println(numArray);


    }
}
